

import ColorType from "./enum/color";

const { ccclass, property } = cc._decorator;

@ccclass
export default class BallCol extends cc.Component {

    @property(cc.Prefab)
    ball: cc.Prefab

    ball_nums: number[] = [];
    colorType: ColorType = ColorType.Red;
    nowXCoe: number
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {

    }


    clear() {
        this.node.removeAllChildren()
    }

    isCanAddBall(num: number, color,o): { canAdd: boolean, pos?: cc.Vec2 } {
        let tempBall = cc.instantiate(this.ball)
        let ballScript = tempBall.getComponent("Ball")
        let x = 0
        let y =100
        ballScript.setPositionBll(cc.v2(x, y))
        if (this.ball_nums.length === 0) {
            this.colorType = color
            ballScript.setBallStyle(color,num)
            this.ball_nums.push(num)
            ballScript.setPositionBll(cc.v2(x, y))
            const pos: cc.Vec2 = this.node.convertToWorldSpaceAR(new cc.Vec2(tempBall.x, tempBall.y))
            // const pos: cc.Vec2 = new cc.Vec2(tempBall.x, tempBall.y)
            // this.node.removeAllChildren()
            return { canAdd: true, pos: pos }
        } else {
            if (this.colorType == color) {
                if (this.ball_nums[this.ball_nums.length - 1] - 1 === num) {
                    let xCoe = this.switchXcoe(parseInt(o.name.substring(7)))
                    
                     x =  (this.ball_nums.length - 1) * xCoe
                     y = 100-((this.ball_nums.length - 1) * 20) 
                    tempBall.x = x;
                    tempBall.y = y;
                    const pos: cc.Vec2 = this.node.convertToWorldSpaceAR(new cc.Vec2(tempBall.x, tempBall.y))
                    // const pos: cc.Vec2 = new cc.Vec2(tempBall.x, y)
                    // this.node.removeAllChildren()
                    this.ball_nums.push(num)
                    return {
                        canAdd: true, pos
                    }
                }

            }
        }


        return { canAdd: false }
    }


    DrawBalls(colIndex: number) {
        this.node.removeAllChildren()
        let xCoe = this.switchXcoe(colIndex)
        this.nowXCoe = xCoe
        for (let i = 0; i < this.ball_nums.length; i++) {

            let ball = cc.instantiate(this.ball)
            let ballScript = ball.getComponent("Ball")
             ballScript.setBallStyle(this.colorType ,this.ball_nums[i])   
            let x =  i * xCoe
            let y = 100-(i * 20) 
            ballScript.setPositionBll(cc.v2(x, y))
            this.node.addChild(ball)
        }

    }

    switchXcoe(colIndex: number): number {
        let xCoe: number = colIndex
        switch (xCoe ) {
            case 0:
                xCoe = -3
                break;
            case 1:
                xCoe = -2
                break;
            case 2:
                xCoe = -1
                break;
            case 3:
                xCoe = 0
                break;
            case 4:
                xCoe = 1
                break;
            case 5:
                xCoe = 2
                break;
            case 6:
                xCoe = 3
                break;
            default:
                break;
        }
        return xCoe
    }
    // update (dt) {}
}
